package com.lly.songhigh.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;


import com.android.volley.toolbox.JsonObjectRequest;

import com.lly.songhigh.R;
import com.lly.songhigh.activity.PlayMusicActivity;
import com.lly.songhigh.adapter.RecommendSongsAdapter;
import com.lly.songhigh.constant.URL;
import com.lly.songhigh.entity.MusicInfo;
import com.lly.songhigh.entity.RankingListDetail;
import com.lly.songhigh.entity.RecommendRoot;
import com.lly.songhigh.entity.RecommendSongsRoot;
import com.lly.songhigh.entity.SingerSingle;
import com.lly.songhigh.eventbus.MyEvent;
import com.lly.songhigh.view.NoScrollGridView;

import com.lly.songhigh.volley.VolleySingleton;


import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by 遇博 on 15/11/03.
 */
public class RecommendSongsFragment extends BaseFragment implements View.OnClickListener {
    private View view;
    private ImageView songs_list_bg;
    private List<RecommendSongsRoot.DataEntity> entityArrayList;
    private VolleySingleton volleySingleton;
    private RecommendSongsAdapter songsAdapter;
    private NoScrollGridView songs_list_listview;
    private List<RecommendRoot.DataEntity> dataEntityArrayList;
    private CharSequence title;
    private int position;
    private List<MusicInfo> musicInfos;



//    @Override
//    protected boolean onBackPressed() {
//        return false;
//    }



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.recommend_detail, null);
        volleySingleton = VolleySingleton.getInstance();
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.coordinator_toolbar);
        toolbar.setNavigationOnClickListener(this);
        toolbar.setTitle(title);
        toolbar.setTitleTextColor(getResources().getColor(R.color.whiteColor));
        songs_list_listview = (NoScrollGridView) view.findViewById(R.id.songs_list_listview);


        getDatas();
        switch (position) {
            case 0:
                getSongsList(URL.URL_LOVE);
                Picasso.with(getActivity()).load(dataEntityArrayList.get(position).getSmallPicUrls().get(0)).into(songs_list_bg);
                break;
            case 1:

                getSongsList(URL.URL_SECOND);
                Picasso.with(getActivity()).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 2:
                getSongsList(URL.URL_THIRD);
                Picasso.with(getActivity()).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 3:
                getSongsList(URL.URL_NEWSONG);
                Picasso.with(getActivity()).load(dataEntityArrayList.get(position).getSmallPicUrls().get(0)).into(songs_list_bg);
                break;
            case 4:
                getSongsList(URL.URL_FIFTH);
                Picasso.with(getActivity()).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 5:
                getSongsList(URL.URL_SIXTH);
                Picasso.with(getActivity()).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 6:
                getSongsList(URL.URL_SEVENTH);
                Picasso.with(getActivity()).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 7:
                getSongsList(URL.URL_EIGHTH);
                Picasso.with(getActivity()).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 8:
                getSongsList(URL.URL_NINETH);
                Picasso.with(getActivity()).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 9:
                getSongsList(URL.URL_TENTH);
                Picasso.with(getActivity()).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 10:
                getSongsList(URL.URL_ELEVENTH);
                Picasso.with(getActivity()).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 11:
                getSongsList(URL.URL_TWELFTH);
                Picasso.with(getActivity()).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
        }

        return view;
    }

    public void getDatas() {
        dataEntityArrayList = (List<RecommendRoot.DataEntity>) getArguments().getSerializable("recommend");
        position = getArguments().getInt("position");
        songs_list_bg = (ImageView) view.findViewById(R.id.songs_list_bg);
        title = dataEntityArrayList.get(position).getName();
    }

    public void getSongsList(String url) {
//        stringRequest = new StringRequestUtf(url, new Response.Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//                Gson gson = new Gson();
//                songsRoot = gson.fromJson(response, RecommendSongsRoot.class);
//                entityArrayList =  songsRoot.getData();
//                songsAdapter = new RecommendSongsAdapter(getActivity(), entityArrayList);
//                songs_list_listview.setAdapter(songsAdapter);
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        });
//        volleySingleton.addQueue(stringRequest);
        JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                // 开始解析
                // 将请求下来的 response转换为字符串
                String str = response.toString();
                // 解析
                // 最外层实体类接收解析结果
                RecommendSongsRoot root = JSON.parseObject(str, RecommendSongsRoot.class);
                entityArrayList =  root.getData();
                songsAdapter = new RecommendSongsAdapter(getActivity(), entityArrayList);
                songs_list_listview.setAdapter(songsAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {


            }
        });
    volleySingleton.addQueue(jr);
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }


}
